package com.example.opengldemo._05_;

import android.opengl.GLES30;
import android.opengl.Matrix;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

public class SixShape {
    public static float[] mMVPMaxtra = new float[16];
    public static float[] mMMaxtra = new float[16];
    public static float[] mVMaxtra = new float[16];
    public static float[] mPMaxtra = new float[16];

    public int m_U_MVPMaxtraHandle;

    public int m_A_PositionHandle;
    public FloatBuffer vertexBuffer;

    public int m_A_ColorHandle;
    public FloatBuffer fragBuffer;

    public int m_U_ColorHandle;
    public float[] fragArray = new float[4];

    public int mProgram;

    public float mShape;
    public float myShape;
    public int vCount;


    public SixShape(MySurfaceView mySurfaceView, float v, float v1, float v2) {
        initShaderSourceData(mySurfaceView);
        initBufferData(v, v1, v2);
    }

    private void initBufferData(float v, float v1, float v2) {
        final float DANWEI = 0.2f;
        vCount = 12;
        float[] vertexArray = getVertexDataArray(v, v1, v2, DANWEI);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertexArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(vertexArray);
        vertexBuffer.position(0);

        float[] fragArray = getFragDataArray();
        byteBuffer = ByteBuffer.allocateDirect(fragArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        fragBuffer = byteBuffer.asFloatBuffer();
        fragBuffer.put(fragArray);
        fragBuffer.position(0);

    }

    private float[] getFragDataArray() {
        float[] colorArray = new float[vCount * 4];//顶点着色数据的初始化
        for (int i = 0; i < vCount; i++) {
            if (i % 3 == 0) { //中心点为白色，RGBA 4 个通道[1,1,1,0]
                colorArray[i * 4] = 1;
                colorArray[i * 4 + 1] = 1;
                colorArray[i * 4 + 2] = 1;
                colorArray[i * 4 + 3] = 0;
            } else { //边上的点为淡蓝色，RGBA 4 个通道[0.45,0.75,0.75,0]
                colorArray[i * 4] = 0.45f;
                colorArray[i * 4 + 1] = 0.75f;
                colorArray[i * 4 + 2] = 0.75f;
                colorArray[i * 4 + 3] = 0;
            }
        }
        return colorArray;
    }

    private float[] getVertexDataArray(float R, float r, float z, float UNIT_SIZE) {
        List<Float> flist = new ArrayList<Float>();
        float tempAngle = 360 / 6;
        for (float angle = 0; angle < 360; angle += tempAngle) {
            flist.add(0f);
            flist.add(0f);
            flist.add(z);

            flist.add((float) (R * UNIT_SIZE * Math.cos(Math.toRadians(angle))));
            flist.add((float) (R * UNIT_SIZE * Math.sin(Math.toRadians(angle))));
            flist.add(z);

            flist.add((float) (r * UNIT_SIZE * Math.cos(Math.toRadians(angle
                    + tempAngle / 2))));
            flist.add((float) (r * UNIT_SIZE * Math.sin(Math.toRadians(angle
                    + tempAngle / 2))));
            flist.add(z);

            flist.add(0f);
            flist.add(0f);
            flist.add(z);

            flist.add((float) (r * UNIT_SIZE * Math.cos(Math.toRadians(angle
                    + tempAngle / 2))));
            flist.add((float) (r * UNIT_SIZE * Math.sin(Math.toRadians(angle
                    + tempAngle / 2))));
            flist.add(z);

            flist.add((float) (R * UNIT_SIZE * Math.cos(Math.toRadians(angle
                    + tempAngle))));
            flist.add((float) (R * UNIT_SIZE * Math.sin(Math.toRadians(angle
                    + tempAngle))));
            flist.add(z);

        }
        vCount = flist.size() / 3;
        float[] vertexArray = new float[flist.size()]; //顶点坐标数组
        for (int i = 0; i < vCount; i++) {
            vertexArray[i * 3] = flist.get(i * 3);
            vertexArray[i * 3 + 1] = flist.get(i * 3 + 1);
            vertexArray[i * 3 + 2] = flist.get(i * 3 + 2);
        }
        return vertexArray;
    }

    private void initShaderSourceData(MySurfaceView mySurfaceView) {
        String vertexRes = ShaderUtil.loadFromAssetsFile("six/vertex.sh", mySurfaceView.getResources());
        String fragRes = ShaderUtil.loadFromAssetsFile("six/fragment.sh", mySurfaceView.getResources());
        mProgram = ShaderUtil.createProgram(vertexRes, fragRes);

        initHandle(mProgram);
    }

    private void initHandle(int mProgram) {
        m_U_MVPMaxtraHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");
        //m_U_ColorHandle = GLES30.glGetUniformLocation(mProgram, "");

        m_A_PositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");
        m_A_ColorHandle = GLES30.glGetAttribLocation(mProgram, "aColor");
    }

    public void drawSelf() {
        GLES30.glUseProgram(mProgram);

        Matrix.setRotateM(mMMaxtra,0,0,0,1,0);
        Matrix.translateM(mMMaxtra,0,0,0,1);
        Matrix.rotateM(mMMaxtra,0,mShape,0,1,0);
        Matrix.rotateM(mMMaxtra,0,myShape,1,0,0);

        GLES30.glUniformMatrix4fv(m_U_MVPMaxtraHandle, 1, false, MatrixState.getFinalMatrix(mMMaxtra), 0);

        GLES30.glVertexAttribPointer(m_A_PositionHandle, 3, GLES30.GL_FLOAT, false, 3 * 4, vertexBuffer);
        GLES30.glVertexAttribPointer(m_A_ColorHandle, 4, GLES30.GL_FLOAT, false, 4 * 4, fragBuffer);

        GLES30.glEnableVertexAttribArray(m_A_ColorHandle);
        GLES30.glEnableVertexAttribArray(m_A_PositionHandle);

        GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount);

        GLES30.glDisableVertexAttribArray(m_A_PositionHandle);
        GLES30.glDisableVertexAttribArray(m_A_ColorHandle);
    }
}
